package com.qiezi.project.activity;


import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qiezi.App;
import com.qiezi.R;
import com.qiezi.base.BaseMvpActivity;
import com.qiezi.base.BaseRecyclerAdapter;
import com.qiezi.bean.DataType;
import com.qiezi.bean.SiteBean;
import com.qiezi.bean.VideoBean;
import com.qiezi.mvp.CreatePresenter;
import com.qiezi.project.adapter.DomesticAdapter;
import com.qiezi.project.contract.CategoryContentContract;
import com.qiezi.project.decoration.RecyclerItemDecoration;
import com.qiezi.project.enums.FooterText;
import com.qiezi.util.Request;

import java.util.ArrayList;

import butterknife.BindView;

@CreatePresenter(presenter = CategoryContentContract.Presenter.class)
public class RecyListActivity extends BaseMvpActivity<CategoryContentContract.Presenter> implements CategoryContentContract.View,View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.tv_back)
    TextView tvBack;

    @BindView(R.id.tv_topBar_title)
    TextView tvTopBarTitle;

    @BindView(R.id.recy)
    RecyclerView rcv;

    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private void initTopbar(String title){
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "fonts/iconfont.ttf");
        tvBack.setTypeface(iconfont);
        tvTopBarTitle.setText(title);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_recycler;
    }
    BaseRecyclerAdapter adapter;


    private SiteBean data;

    private int type;

    @Override
    protected void onSaveInstanceState(Bundle paramBundle) {
        super.onSaveInstanceState(paramBundle);
        if (paramBundle == null)return;

        paramBundle.putInt("type",type);
        paramBundle.putSerializable("data",data);
    }

    @Override
    protected void initData(Bundle saveBundle) {

        if (saveBundle != null){
            type = saveBundle.getInt("type");
            data = (SiteBean) saveBundle.getSerializable("data");
        }else {
            this.data = (SiteBean) getIntent().getSerializableExtra("data");
            type = getIntent().getIntExtra("type",0);
        }

        initTopbar(data.getName());

        rcv.setLayoutManager(new GridLayoutManager(this,2));
        adapter = new DomesticAdapter().setType(type);
        adapter.setEmptyViewDef(this,R.drawable.ic_network_error);
        adapter.setFooterViewDef(this);
        rcv.setAdapter(adapter);
        rcv.setItemAnimator(new DefaultItemAnimator());
        rcv.addItemDecoration(new RecyclerItemDecoration(10,2));

        refresh.setColorSchemeResources(R.color.colorRed);
        refresh.setOnRefreshListener(this);
        refresh.setRefreshing(true);
        onRefresh();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
        }
    }


    private boolean isLoad = false;

    @Override
    protected void initListener() {
        tvBack.setOnClickListener(this);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<VideoBean>() {
            @Override
            public void onItemClick(int position, VideoBean data) {
                log(data.toString());
                Intent intent;
                if (data.getTypedata() == DataType.video){
                    intent = new Intent(RecyListActivity.this,PlayActivity.class);
                    intent.putExtra("data",data);
                }else {
                    intent = new Intent(RecyListActivity.this,WebActivity.class);
                    intent.putExtra("data",data);
                }
                startActivity(intent);

            }
        });

        rcv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            private boolean isLittle = false;
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!isLittle){
                    isLittle = !(recyclerView.getLayoutManager().getChildCount() == recyclerView.getLayoutManager().getItemCount());
                }
                if (isLittle && isLoad && footerText != FooterText.loading && footerText != FooterText.notMore) {
                    if (App.getInstance().isVip){
                        if (isSlideToBottom(rcv)){
                            setFooterText(FooterText.loading);
                            getPresenter().loadMore(data.getId(),++page);
                        }else if (footerText != FooterText.loadMore){
                            setFooterText(FooterText.loadMore);
                        }
                    }else if (footerText != FooterText.notVip){
                        setFooterText(FooterText.notVip);
                    }
                }
            }
        });
    }

    private int page = 1;

    public void complete(FooterText status){

        setFooterText(status);
        complete();
    }

    @Override
    public void complete() {
        super.complete();
        isLoad = true;
        if (refresh != null && refresh.isRefreshing()){
            refresh.setRefreshing(false);
        }
        if (adapter.getEmptyVisibility() == View.INVISIBLE){
            adapter.setEmptyVisibility(View.VISIBLE);
        }

    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        adapter.setEmptyImage(R.drawable.ic_network_error);
        complete(FooterText.loadError);
    }

    private FooterText footerText = FooterText.notMore;

    private void setFooterText(FooterText ft){
        footerText = ft;
        adapter.setFooterText(ft.getString());
    }

    @Override
    public void onLoadMore(ArrayList<VideoBean> data) {
        if (data.size() == 0){
            complete(FooterText.notMore);
        }else {
            adapter.addDatas(data);
            complete(FooterText.complete);
        }
    }

    @Override
    public void onRefresh(ArrayList<VideoBean> data) {
        log("size:"+data.size());
        if (data.size() == 0){
            adapter.setEmptyImage(R.drawable.ic_data_empty);
        }
        adapter.setDatas(data);
        complete(FooterText.empty);
    }

    @Override
    public Request.Api getApi() {
        return type == 0 ? Request.Api.getResourceList : Request.Api.getSiteList;
    }

    @Override
    protected void onDestroy() {
        getPresenter().canceledCell();
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getPresenter().loadData(data.getId(),page);
    }
}
